package common

import (
	"context"
	"mini-app/internal/app/grpc/client"
	"mini-app/internal/app/grpc/protoc/sale_adminc"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/logx"
)

// OrderShipmentInfo 物流信息
func (GRPC) OrderShipmentInfo(ctx context.Context, outTradeNo string) (expressNum, expressCom string) {
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.ShipmentInfo(ctx, &sale_adminc.ShipmentInfoReq{
			OutTradeNo: outTradeNo,
		})
		if rsp != nil && err == nil {
			return rsp.ExpressNum, rsp.ExpressCom
		}
	}
	return "", ""
}

// OrderShippingTracking 物流轨迹
func (GRPC) OrderShippingTracking(ctx context.Context, outTradeNo string) []string {
	cli, err := client.GetSaleAdminClient()
	if cli != nil && err == nil {
		rsp, err := cli.ShippingTracking(ctx, &sale_adminc.ShippingTrackingReq{
			OutTradeNo: outTradeNo,
		})
		if rsp != nil && err == nil {
			return rsp.Tracking
		}
	}
	return []string{}
}

// 获取运费
func (GRPC) GetExpressFee(ctx context.Context, goodsID, provinceName string) (expressFee float64, err error) {
	saleAdminClient, err := client.GetSaleAdminClient()
	if saleAdminClient != nil && err == nil {
		rsp, err := saleAdminClient.ExpressFee(
			ctx,
			&sale_adminc.ExpressFeeReq{
				GoodsId:  goodsID,
				Province: provinceName,
			},
		)
		if err != nil {
			logx.Error(context.Background(), "get ExpressFee error", logx.Err(err))
			err = errorx.New("获取运费出错", -1)
			return 9999.99, err
		}
		if rsp.Msg != "" {
			err = errorx.New(rsp.Msg, -1)
			return 9999.99, err
		}
		return rsp.Fee, nil
	}
	return 9999.99, errorx.New("获取运费出错")
}
